//
//  SXTTabBarViewController.m
//  SYT
//
//  Created by 彭之耀 on 2017/8/23.
//  Copyright © 2017年 个人学习. All rights reserved.
//

#import "SXTTabBarViewController.h"
#import "SXTNavigationController.h"

@interface SXTTabBarViewController ()

@property (strong,nonatomic) NSArray *tabBarMessageArray;//存储plist文件的数据

@end

@implementation SXTTabBarViewController

-(NSArray *)tabBarMessageArray{
    if (!_tabBarMessageArray) {
        _tabBarMessageArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"SXTTabBarViewController" ofType:@"plist"]];
    }
    return _tabBarMessageArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //通过appearance设置当前tabbar的样式
    //方法注释后有UI_APPEARANCE_SELECTOR，都可以用appearance设置
    UITabBarItem *item = [UITabBarItem appearance];
    [item setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10.0f],
                                  NSForegroundColorAttributeName:[UIColor grayColor]} forState:UIControlStateNormal];
    [item setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10.0f],
                                   NSForegroundColorAttributeName:[UIColor colorWithRed:57.0/255.0 green:167.0/255.0 blue:241.0/255 alpha:1]} forState:UIControlStateSelected];
    /*
     原始状态
     */
//    //第一子视图
//    UIViewController *viewController1 = [[UIViewController alloc]init];
//    
//    viewController1.view.backgroundColor = [UIColor redColor];
//    viewController1.tabBarItem.image = [UIImage imageNamed:@"菜单栏限时特卖按钮未选中状态"];
//    viewController1.tabBarItem.selectedImage = [UIImage imageNamed:@"菜单栏限时特卖按钮选中状态"];
//    viewController1.tabBarItem.title = @"限时购";
//    //    [tabbarController addChildViewController:viewController1];
//    
//    //第二子视图
//    UIViewController *viewController2 = [[UIViewController alloc]init];
//    viewController2.view.backgroundColor = [UIColor blueColor];
//    //    [tabbarController addChildViewController:viewController2];
//    viewController2.tabBarItem.image = [UIImage imageNamed:@"菜单栏分类按钮未选中状态"];
//    viewController2.tabBarItem.selectedImage = [UIImage imageNamed:@"菜单栏分类按钮选中状态"];
//    viewController2.tabBarItem.title = @"分类";
//    
//    //第三子视图
//    UIViewController *viewController3 = [[UIViewController alloc]init];
//    viewController3.view.backgroundColor = [UIColor yellowColor];
//    //    [tabbarController addChildViewController:viewController3];
//    viewController3.tabBarItem.image = [UIImage imageNamed:@"菜单栏购物车按钮未选中状态"];
//    viewController3.tabBarItem.selectedImage = [UIImage imageNamed:@"菜单栏购物车按钮选中状态"];
//    viewController3.tabBarItem.title = @"购物车";
//    
//    //第四子视图
//    UIViewController *viewController4 = [[UIViewController alloc]init];
//    viewController4.view.backgroundColor = [UIColor greenColor];
//    //    [tabbarController addChildViewController:viewController4];
//    viewController4.tabBarItem.image = [UIImage imageNamed:@"菜单栏我的按钮未选中状态"];
//    viewController4.tabBarItem.selectedImage = [UIImage imageNamed:@"菜单栏我的按钮选中状态"];
//    viewController4.tabBarItem.title = @"我的";
//    
//    self.viewControllers = @[viewController1,viewController2,viewController3,viewController4];
    /*
     优化一
     */
//    NSArray *tabbarImage = @[@"菜单栏限时特卖按钮未选中状态",@"菜单栏分类按钮未选中状态",@"菜单栏购物车按钮未选中状态",@"菜单栏我的按钮未选中状态"];
//    NSArray *tabbarSelectImage = @[@"菜单栏限时特卖按钮选中状态",@"菜单栏分类按钮选中状态",@"菜单栏购物车按钮选中状态",@"菜单栏我的按钮选中状态"];
//    NSArray *tabbarTitle = @[@"限时购",@"分类",@"购物车",@"我的"];
//    
//    SXTTimeViewController *viewController1 = [[SXTTimeViewController alloc]init];
//    SXTClassViewController *viewController2 = [[SXTClassViewController alloc]init];
//    SXTBuyViewController *viewController3 = [[SXTBuyViewController alloc]init];
//    SXTMyViewController *viewController4 = [[SXTMyViewController alloc]init];
//    NSArray *tabbarChildController = @[viewController1,viewController2,viewController3,viewController4];
//    NSDictionary *tabbarDic = @{@"tabbarImage":tabbarImage,@"tabbarSelectImage":tabbarSelectImage,@"tabbarTitle":tabbarTitle,@"tabbarChildController":tabbarChildController};
//    for (int i = 0; i<4; i++) {
//        UIViewController *viewController = [tabbarChildController objectAtIndex:i];
//        viewController.tabBarItem.image = [UIImage imageNamed:[tabbarImage objectAtIndex:i]];
//        viewController.tabBarItem.selectedImage = [UIImage imageNamed:[tabbarSelectImage objectAtIndex:i]];
//        viewController.tabBarItem.title = [tabbarDic[@"tabbarTitle"] objectAtIndex:i];
//        [self addChildViewController:viewController];
//    }
    
    /*
     优化二
     */
    for (NSDictionary *dic in self.tabBarMessageArray) {
        Class viewControllerClass = NSClassFromString(dic[@"tabbarController"]);
        UIViewController *viewController = [[viewControllerClass alloc]init];
        viewController.tabBarItem.image = [UIImage imageNamed:dic[@"tabbarImage"]];
        viewController.tabBarItem.selectedImage = [UIImage imageNamed:dic[@"tabbarSelectImage"]];
//        viewController.tabBarItem.title = dic[@"tabbarTitle"];
        viewController.title = dic[@"tabbarTitle"];
        SXTNavigationController *navgation = [[SXTNavigationController alloc]initWithRootViewController:viewController];
        [self addChildViewController:navgation];
    }
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
